﻿using Consul;
using MediatR;
using RedDragon.Common.Domain.InventoryAllocate;
using RedDragon.Common.Domain.warehouse;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.ProcessW;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Inventoryscheduling
{
    public class InventorySearchHandler : IRequestHandler<InventorySearchCommon, Result>
    {
        public readonly IBaseRepository<Inventory> InventoryRepository;
        public readonly IBaseRepository<Warehouse> WarehouseRepository;

        public InventorySearchHandler(IBaseRepository<Inventory> inventoryRepository, IBaseRepository<Warehouse> warehouseRepository)
        {
            InventoryRepository = inventoryRepository;
            WarehouseRepository = warehouseRepository;
        }

        public async Task<Result> Handle(InventorySearchCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "" };
            try
            {
                var list = (from a in InventoryRepository.Query()
                            join
                            b in WarehouseRepository.Query()
                            on a.SourcerepositoryId equals b.WarehouseId
                            select new InventoryDto()
                            {
                                InventoryId = a.InventoryId,
                                AllocateNumber = a.AllocateNumber,
                                AllocateDate = a.AllocateDate,
                                SourcerepositoryId = a.SourcerepositoryId,
                                SourcerepositoryName = b.WarehouseName,
                                SourcerepositoryAdd = b.WarehouseAddr,
                                TargetrepositoryId = a.TargetrepositoryId,
                                TargetrepositoryName = WarehouseRepository.Query().Where(x => x.WarehouseId == a.TargetrepositoryId).FirstOrDefault().WarehouseName,
                                TargetrepositoryAdd = WarehouseRepository.Query().Where(x => x.WarehouseId == a.TargetrepositoryId).FirstOrDefault().WarehouseAddr,
                                Serialnumber = b.Serialnumber,
                                Status = a.Status,
                                StatusName = a.Status == 1 ? "新增" : a.Status == 2 ? "变更" : a.Status == 2 ? "确认" : "取消",
                                Remark = a.Remark,
                                CreateName = a.CreateName,
                                MarkdepartmentName = a.Markdepartment == 1 ? "开发部" : a.Markdepartment == 2 ? "管理部" : "外交部",
                                CreateTime = a.CreateTime,
                                produceplanAuditName = a.produceplanAudit == 0 ? "未提交" : a.produceplanAudit == 1 ? "已提交" : a.produceplanAudit == 2 ? "已驳回" : a.produceplanAudit == 3 ? "已审批" : "变更"
                            }).ToList();
                //调拨编码
                if (request.AllocateNumber != null)
                {
                    list = list.Where(x => x.AllocateNumber == request.AllocateNumber).ToList();
                }
                //来源仓库
                if (request.SourcerepositoryId != -1)
                {
                    list = list.Where(x => x.SourcerepositoryId == request.SourcerepositoryId).ToList();
                }
                //目标仓库
                if (request.TargetrepositoryId != -1)
                {
                    list = list.Where(x => x.TargetrepositoryId == request.TargetrepositoryId).ToList();
                }
                //调拨日期
                if (request.AllocateDate != null)
                {
                    list = list.Where(x => x.AllocateDate == request.AllocateDate).ToList();
                }
                //状态
                if (request.Status != -1)
                {
                    list = list.Where(x => x.Status == request.Status).ToList();
                }
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
    public class InventoryDto
    {
        public int InventoryId { get; set; }
        /// <summary>
        /// 调拨编码
        /// </summary>
        public string? AllocateNumber { get; set; }
        /// <summary>
        /// 调拨日期
        /// </summary>
        public DateTime AllocateDate { get; set; }
        /// <summary>
        /// 来源仓库id
        /// </summary>
        public int SourcerepositoryId { get; set; }
        /// <summary>
        /// 来源仓库名称
        /// </summary>
        public string? SourcerepositoryName { get; set; }
        /// <summary>
        /// 来源仓库地址
        /// </summary>
        public string? SourcerepositoryAdd { get; set; }
        /// <summary>
        /// 目标仓库Id
        /// </summary>
        public int TargetrepositoryId { get; set; }
        /// <summary>
        /// 目标仓库名称
        /// </summary>
        public string? TargetrepositoryName { get; set; }
        /// <summary>
        /// 目标仓库地址
        /// </summary>
        public string? TargetrepositoryAdd { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 状态名称
        /// </summary>
        public string? StatusName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark { get; set; }
        /// <summary>
        /// 制单人
        /// </summary>
        public string? CreateName { get; set; }
        /// <summary>
        /// 制单部门
        /// </summary>
        public int Markdepartment { get; set; }
        /// <summary>
        /// 制单部门
        /// </summary>
        public string? MarkdepartmentName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 审批状态
        /// </summary>
        public int produceplanAudit { get; set; }
        /// <summary>
        /// 审批状态
        /// </summary>
        public string? produceplanAuditName { get; set; }
        public string Serialnumber { get; set; }
    }
}
